package myapp.Chat;

import java.util.List;

import staticClass.XmppConnection;

import myapp.DTO.UserDTO;
import myapp.EnumAndConstans.Constants;
import myapp.googlemap.R;
import myapp.services.ChatServiceHelper;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FriendListAdapter extends ArrayAdapter<UserDTO>{

	private int resource;
	private FriendListDialog dialog = null;
	public FriendListAdapter(Context context, int textViewResourceId,
			List<UserDTO> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.resource = textViewResourceId;
	}
	
	public FriendListAdapter(Context context, int textViewResourceId,
			List<UserDTO> objects, FriendListDialog dialog) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.resource = textViewResourceId;
		this.dialog = dialog;
	}
	
//	public UserDTOAdapter(Context context, int resource,
//			int textViewResourceId, List<UserDTO> objects) {
//		super(context, resource, textViewResourceId, objects);
//		// TODO Auto-generated constructor stub
//		this.resource = resource;
//	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LinearLayout userView;
		final UserDTO friend = getItem(position);
		
		if (convertView == null){
			userView = new LinearLayout(getContext());
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(inflater);
			vi.inflate(resource, userView, true);
		}
		else{
			userView = (LinearLayout) convertView;
		}
		
		//Get View of txt will contain data
		TextView userName = (TextView) userView.findViewById(R.id.txtName);
		TextView userEmail = (TextView) userView.findViewById(R.id.txtEmail);
		
		//Assign data to txt
		userName.setText(friend.name);
		userEmail.setText(friend.email);
		if (friend.available == Constants.NOT_AVAILABLE){
			Button btnOnline = (Button) userView.findViewById(R.id.btnUserOnline);
			btnOnline.setVisibility(View.INVISIBLE);
		}
		
		Button btnRemoveFriend = (Button) userView.findViewById(R.id.btnRemoveFriend);
		btnRemoveFriend.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(final View v) {
				// TODO Auto-generated method stub
				//AlertDialog alert =
				new AlertDialog.Builder(getContext())
					.setIcon(R.drawable.icon_attention)
					.setTitle(R.string.chat_confirm_title_delete_friend)
					.setMessage(R.string.chat_confirm_msg_delete_friend)
					.setPositiveButton(R.string.chat_confirm_yes_button, new DialogInterface.OnClickListener() {						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							RemoveFriend(XmppConnection.userEmail, friend.email);
							VisualizeBtnRemoveFriendClicked(v);
						}
					} )
					.setNegativeButton(R.string.chat_confirm_no_button, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							dialog.dismiss();
						}
					})
					.show();
//				RemoveFriend(XmppConnection.userEmail, friend.email);
//				VisualizeBtnRemoveFriendClicked(v);
			}
		});
		
		userView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.i("FriendListAdapter", "[userView] onclicked");
				if (dialog != null)
					dialog.itemClickedCallback(friend);
			}
		});
		
		return userView;
	}
	
	private void RemoveFriend(String userEmail, String friendEmail){
		ChatServiceHelper chatHelper = new ChatServiceHelper();
		chatHelper.RemoveFriend(userEmail, friendEmail);
	}
	
	private void VisualizeBtnRemoveFriendClicked(View btn){
		try {
			View listViewItem = (View) btn.getParent().getParent();
			listViewItem.setVisibility(View.GONE);
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("RequesterAdapter", "[ProcessRejectButtonClicked] exception:" + e.getMessage() + e.getLocalizedMessage() + e.getCause());
		}
	}
}
